package com.yisu.auction.api.core.service.impl.func;

import com.alibaba.fastjson.JSON;
import com.google.common.collect.Maps;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.yisu.auction.api.core.service.IMobileRechargeService;
import com.yisu.auction.api.core.service.ISystemConfigService;
import com.yisu.auction.api.domain.config.SystemConfig;
import com.yisu.auction.api.domain.request.operatorBusiness.TelephoneChargeRequest;
import com.yisu.auction.api.domain.response.rechargePackge.BalanceListResponse;
import com.yisu.auction.api.domain.response.rechargePackge.TrafficListResponse;
import com.yisu.auction.api.domain.vo.configJsonVos.BalanceJsonVo;
import com.yisu.auction.api.domain.vo.configJsonVos.RechargeTrafficAmountVo;
import com.yisu.auction.api.domain.vo.configJsonVos.TrafficJsonVo;
import com.yisu.auction.api.domain.vo.indexPackageVos.SystemConfigVo;
import com.yisu.auction.api.domain.vo.mobileChargePackageVos.RechargeResponse;
import com.yisu.auction.ewf.common.utils.MD5Util;
import com.yisu.auction.ewf.common.web.HttpWebClient;
import com.yisu.auction.ewf.entity.MobileRecharge;
import org.springframework.stereotype.Controller;

import javax.annotation.Resource;
import java.lang.reflect.Type;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * author: 赵杰
 * Business Description：
 * createTime：2017/9/2
 */
@Controller
public class MobileChargeUtil {

    @Resource
    private IMobileRechargeService iMobileRechargeService;

    @Resource
    private ISystemConfigService iSystemConfigService;

    @Resource
    private SystemConfig systemConfig;

    private SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddHHmmssSSS");
    private String servicenumber;
    private String face;
    private String pkgtype;
    private String agentbillno;
    private String sign;

    public MobileChargeUtil() {
    }

    public MobileChargeUtil(String agentbillno) {
        this.agentbillno = agentbillno;
    }

    /**
     * 流量充值
     *
     * @return
     */
    public boolean FlowRecharge(String orderNo) {
        RechargeResponse rechargeResponse = new RechargeResponse();
        MobileRecharge mobileRecharge = iMobileRechargeService.getMobileRechargeByOrderNo(orderNo);
        Map<String, String> params = Maps.newConcurrentMap();
        params.put("accountname", systemConfig.getMobileChargeAccountName());
        params.put("servicenumber", mobileRecharge.getMobile());
        params.put("face", String.valueOf(mobileRecharge.getFace()));
        params.put("agentbillno", orderNo);
        String signStr = systemConfig.getMobileChargeAccountName() + mobileRecharge.getFace() + orderNo + systemConfig.getMobileChargePassword();
        sign = MD5Util.encode(signStr);
        params.put("sign", sign.toLowerCase());
        String jsonResult = "";
        if (mobileRecharge.getRechargeType() == 2) {
            params.put("pkgtype", String.valueOf(mobileRecharge.getPkgType()));
            jsonResult = HttpWebClient.post(systemConfig.getMobileChargeInterfaceUrl() + "/mobileflow/recharge", params);
        }
        if (mobileRecharge.getRechargeType() == 1) {
            jsonResult = HttpWebClient.post(systemConfig.getMobileChargeInterfaceUrl() + "/recharge", params);
        }
        rechargeResponse = JSON.parseObject(jsonResult, RechargeResponse.class);
        if (rechargeResponse != null) {
            if (rechargeResponse.getErrorcode().equals("0")) {
                return true;
            }
        }
        return false;
    }

    /**
     * 获取流量充值配置JSON
     *
     * @param id
     * @return
     */
    public TrafficJsonVo getTrafficJson(String id) {
        SystemConfigVo systemConfigVo = iSystemConfigService.getSystemConfigByKey("trafficJson");
        Gson gson = new Gson();
        Type type = new TypeToken<List<TrafficJsonVo>>() {
        }.getType();
        List<TrafficJsonVo> trafficJsonVos = gson.fromJson(systemConfigVo.getConfigValue(), type);
        for (TrafficJsonVo t : trafficJsonVos) {
            if (t.getId().equals(id)) {
                return t;
            }
        }
        return null;
    }

    /**
     * 获取话费充值配置
     *
     * @param id
     * @return
     */
    public BalanceJsonVo getBalanceJson(String id) {
        SystemConfigVo systemConfigVo = iSystemConfigService.getSystemConfigByKey("balanceJson");
        Gson gson = new Gson();
        Type type = new TypeToken<List<BalanceJsonVo>>() {
        }.getType();
        List<BalanceJsonVo> balanceJsonVos = gson.fromJson(systemConfigVo.getConfigValue(), type);
        for (BalanceJsonVo t : balanceJsonVos) {
            if (t.getId().equals(id)) {
                return t;
            }
        }
        return null;
    }

    /**
     * 获取充值支付金额
     *
     * @param id
     * @param rechargeType
     * @param pkgType
     * @return
     */
    public void setRechargeAmountAndFace(String id, Integer rechargeType, Integer pkgType, TelephoneChargeRequest telephoneChargeRequest) {
        if (MobileRecharge.RechargeTypeEnum.Balance.code == rechargeType) {
            BalanceJsonVo balanceJsonVo = getBalanceJson(id);
            if (balanceJsonVo == null) {
                telephoneChargeRequest.setAmount(0);
            }
            telephoneChargeRequest.setAmount(balanceJsonVo.getAmount());
            telephoneChargeRequest.setFace(balanceJsonVo.getFace());
            telephoneChargeRequest.setDescription(balanceJsonVo.getText());
        } else {
            TrafficJsonVo trafficJsonVo = getTrafficJson(id);
            if (trafficJsonVo == null) {
                telephoneChargeRequest.setAmount(0);
            }
            if (MobileRecharge.PkgTypeEnum.NationWide.code == pkgType) {
                telephoneChargeRequest.setAmount(trafficJsonVo.getNationWide().getAmount());
                telephoneChargeRequest.setDescription(MobileRecharge.PkgTypeEnum.NationWide.name + "流量:" + trafficJsonVo.getText());
            } else {
                telephoneChargeRequest.setAmount(trafficJsonVo.getProvince().getAmount());
                telephoneChargeRequest.setDescription(MobileRecharge.PkgTypeEnum.Province.name + "流量:" + trafficJsonVo.getText());
            }
            telephoneChargeRequest.setFace(trafficJsonVo.getFace());
        }
    }

    /**
     * 获取流量充值列表
     *
     * @return
     */
    public List<TrafficListResponse> getTrafficList(int useState) {
        SystemConfigVo systemConfigVo = iSystemConfigService.getSystemConfigByKey("trafficJson");
        Gson gson = new Gson();
        Type type = new TypeToken<List<TrafficJsonVo>>() {
        }.getType();
        List<TrafficJsonVo> trafficJsonVos = gson.fromJson(systemConfigVo.getConfigValue(), type);
        List<TrafficListResponse> trafficListResponses = new ArrayList<>();
        for (TrafficJsonVo trafficJsonVo : trafficJsonVos) {
            if (trafficJsonVo.isUserState()) {
                TrafficListResponse trafficListResponse = new TrafficListResponse();
                trafficListResponse.setId(trafficJsonVo.getId());
                trafficListResponse.setText(trafficJsonVo.getText());
                trafficListResponse.setAmount(trafficJsonVo.getProvince().getAmount());
                List<RechargeTrafficAmountVo> rechargeTrafficAmountVos = new ArrayList<>();
                rechargeTrafficAmountVos.add(trafficJsonVo.getNationWide());
                rechargeTrafficAmountVos.add(trafficJsonVo.getProvince());
                trafficListResponse.setPkgList(rechargeTrafficAmountVos);
                trafficListResponse.setUseState(useState);
                trafficListResponses.add(trafficListResponse);
            }
        }
        return trafficListResponses;
    }

    /**
     * 获取话费充值列表
     *
     * @return
     */
    public List<BalanceListResponse> getBalanceList(int useState) {
        SystemConfigVo systemConfigVo = iSystemConfigService.getSystemConfigByKey("balanceJson");
        Gson gson = new Gson();
        Type type = new TypeToken<List<BalanceJsonVo>>() {
        }.getType();
        List<BalanceJsonVo> balanceJsonVos = gson.fromJson(systemConfigVo.getConfigValue(), type);
        List<BalanceListResponse> balanceListResponses = new ArrayList<>();
        for (BalanceJsonVo balanceJsonVo : balanceJsonVos) {
            if (balanceJsonVo.isUserState()) {
                BalanceListResponse balanceListResponse = new BalanceListResponse();
                balanceListResponse.setId(balanceJsonVo.getId());
                balanceListResponse.setText(balanceJsonVo.getText());
                balanceListResponse.setAmount(balanceJsonVo.getAmount());
                balanceListResponse.setAmountText(balanceJsonVo.getAmountText());
                balanceListResponse.setUseState(useState);
                balanceListResponses.add(balanceListResponse);
            }
        }
        return balanceListResponses;
    }

    /**
     * 获取支持的省份
     *
     * @return
     */
    public String getSupportProvice() {
        SystemConfigVo systemConfigVo = iSystemConfigService.getSystemConfigByKey("rechargeSupportProvince");
        return systemConfigVo.getConfigValue();
    }
}
